package top.fkxuexi.smartwork.tools;

import top.fkxuexi.smartwork.config.GenerConfig;
import top.fkxuexi.smartwork.config.SmkConfig;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class FileUtils {
    private static SimpleDateFormat sdf = new SimpleDateFormat("HH-ss-mm");


    public static FileWriter getFileWriter(GenerConfig config,SmkConfig cfg,String javaDbName) throws IOException {

        String path;
        if (config.isMapper()){
            path = getResourcePath() + cfg.getXmlCodeLocation()+"/" ;
        }else{
            path = getRootPath() + cfg.getCodeBasePath() + "/" + config.getPackageName() +"/"  ;
        }
        path = path.replace("//","/").replace("\\","/").replace(".","/");
        path += javaDbName+config.getClazzSuffix()+config.getFileSuffix();
        File file = new File(path);
        if (!file.exists()){
            File parentFile = file.getParentFile();
            file.getParentFile().mkdirs();
        }else{
            file.renameTo(new File(path+sdf.format(new Date())));
        }

        return new FileWriter(file);
    }

    public static String  getRootPath(){
        /**
         * 这个地方是优先的获取程序的运行的路径是没有错的
         */
        return System.getProperty("user.dir")+"/src/main/java/";
    }
    public static String getResourcePath(){
        return System.getProperty("user.dir")+"/src/main/resources/";
    }



}
